// pages/home/follow/follow.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: null,
    beforeIndex:null,
    followList: [],
    hotList: [],
    timer: null,
    editThis: true,
    InitData: false,
    hotFun:'',
    lastIndex:null,
    brandId: -1,
    brandId1: -1
    // seeMore: false,
    // brandList:{},//品牌的索引列表
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.InitData) {
      this.carBrandAttentionList();
    }
    const _this = this
  },
  /**
   * 生命周期函数--监听页面加载
   */
  doNothing() {},
  async onLoad(options) {
    await this.carBrandAttentionList();
    await this.carBrandHotList();
    this.setData({
      'InitData': true
    })
  },
  // 获取关注列表
  async carBrandAttentionList() {
    const res = await app.sHttp.carBrandAttentionStat()
    this.setData({
      followList: res.attentionList
    });
  },
  // 热门品牌列表
  carBrandHotList() {
    app.sHttp.carBrandHotList().then(res => {
      // 热门list
      let hotList = res.brandList
      // 对比list去除重复数据
      hotList = hotList.filter((item1) => {
        return this.data.followList.every((item2) => {
          return item2.brandId !== item1.brandId
        })
      })
      this.setData({
        hotList
      });
    }, err => {
      app.error(err.msg);
    })
  },
  // 移除关注列表
  removeFollow(e) {
    let index = e.currentTarget.dataset.idx;
    let item = e.currentTarget.dataset.item;
    this.setData({
      activeIndex: null,
      beforeIndex:index,
      brandId1: item.brandId
    })
    let followList = this.data.followList;
    let hotList = this.data.hotList;
    var that = this
    // 数组内移除
    that.data.hotFun= setTimeout(function(){
    app.sHttp.carBrandAttention({'operateType': 2,'brandId': followList[index].brandId}).then(res => {
      hotList.push(followList[index])
      followList.splice(index, 1);
      that.setData({
        followList,
        hotList,
        beforeIndex:null,
        brandId:-1
      })
    }, err => {
      app.error(err.msg);
    })
   },500)
  },
  // 加入关注嗷.
  addFollow(e) {
    var that = this 
    let followList = this.data.followList;
    let index = e.currentTarget.dataset.idx;
    let item = e.currentTarget.dataset.item;

    console.log(item)
    this.setData({
      activeIndex: index,
      brandId: item.brandId
    })
    let hotList = this.data.hotList;
    that.data.hotFun = setTimeout(function () {
      app.sHttp.carBrandAttention({'operateType': 1,'brandId': hotList[index].brandId}).then(res => {
        followList.push(hotList[index]);
        hotList.splice(index, 1); // 更新hotList数组
        clearInterval(that.data.hotFun);
        that.setData({
          hotList: hotList,
          activeIndex: null,
          followList: followList,
          size: 1,
          brandId1:-1
        })
      }, err => {
        app.error(err.msg);
      });
    }, 500)
  },
  
  // 回调回来的.加入关注
  // brandClick(e){
  //     // 回调回来查看是否处于编辑状态.打开编辑状态.
  //     if(!this.data.editThis){
  //         this.setData({
  //             editThis: true
  //         })
  //     }
  //     // 加入关注列表
  //     let chooseBrand = e.detail;
  //     let followList = this.data.followList;
  //     app.sHttp.carBrandAttention({'operateType':1,'brandId':chooseBrand.brandId}).then(res =>{
  //       followList.push(chooseBrand);
  //       this.setData({
  //           followList:followList
  //       });
  //       app.alert('关注成功');
  //     },err=>{
  //       app.error(err.msg);
  //     });
  // },
  // getCarBrandList(){
  //   if(null == app.globalData.carBrandList){
  //       app.sHttp.carBrandList().then(res =>{
  //           let brandList = {};
  //           res.brandList.forEach(item =>{
  //               if(undefined == brandList[item.firstLetter]){
  //                   brandList[item.firstLetter] = [item]
  //               }else{
  //                   brandList[item.firstLetter].push(item);
  //               }
  //           });
  //           app.globalData.carBrandList = brandList;
  //           this.setData({
  //               'brandList': brandList
  //           });
  //           this.switchSeeMore();
  //       },err=>{
  //           app.error(err.msg);
  //       })
  //   }else{
  //       this.setData({
  //           'brandList': app.globalData.carBrandList
  //       });
  //       this.switchSeeMore();
  //   }
  // },
  jumpToMore() {
    wx.navigateTo({
      url: '/pages/home/follow/more/more',
    })
  },
  switchFollow() {
    let self = this;
    self.setData({
      editThis: !self.data.editThis
    })
  },
  BackPage() {
    var that = this;
    wx.navigateBack({
      delta: 1,
      fail: function () {
        that.toHome();
      }
    });
  },
  toHome() {
    wx.reLaunch({
      url: '/pages/home/home',
    })
  },
  // 跳转到品牌搜索并立即搜索对应品牌数据
  jumpToFollowBrand(e) {
    if (0 != e.currentTarget.dataset.carnums) {
      wx.navigateTo({
        url: '/pages/home/search/search?etype=brand&show=1&bid=' + e.currentTarget.dataset.bid,
      })
    } else {
      app.alert('该品牌暂无车辆');
    }
  },
  // switchSeeMore(){
  //     let self = this;
  //     this.setData({
  //         seeMore: !self.data.seeMore
  //     });
  //     // 滚动到顶部
  //     if(wx.pageScrollTo){
  //         wx.pageScrollTo({
  //           scrollTop:0,
  //           duration: 0,
  //         })
  //     }
  // },
})